<div style="padding-bottom: 20px;">

    <?php $this->widget('admin.components.widgets.AlertWidget');?>

    <?php $form=$this->beginWidget('CActiveForm', array(
                'id'=>'file-form',
                'enableClientValidation'=>true,
                'clientOptions'=>array(
                    'validateOnSubmit'=>true,
                ),
                //'enableAjaxValidation'=>true,
                'htmlOptions' => array('class' => 'da-form', 'enctype' => 'multipart/form-data')
            )); ?>

    <?php echo $form->errorSummary($model); ?>
    <div class="da-form-inline">
        <div class="da-form-row" style="margin-top: 15px;">
            <div class="da-form-col-4-8">
                <?php echo $form->labelEx($model,'type'); ?>
                <div class="da-form-item large">
                    <?php echo $form->dropDownList($model, 'type', File::model()->typeData); ?>
                    <?php echo $form->error($model,'type');?>
                </div>
            </div>
        </div>
        <div class="da-form-row">
            <div class="da-form-col-4-8">
                <?php echo $form->labelEx($model,'name'); ?>
                <div class="da-form-item large">
                    <?php echo $form->textField($model,'name',array('maxlength'=>250)); ?>
                    <?php echo $form->error($model,'name');?>
                </div>
            </div>
        </div>

        <div class="da-form-row">
            <div class="da-form-col-4-8">
                <?php echo $form->labelEx($model,'desc'); ?>
                <div class="da-form-item large">
                    <?php echo $form->textArea($model, 'desc'); ?>
                    <?php echo $form->error($model,'desc');?>
                </div>
            </div>
        </div>
        
        <div class="da-form-row">
            <div class="da-form-col-4-8">
                <?php echo $form->labelEx($model,'alias'); ?>
                <div class="da-form-item large">
                    <?php echo $form->textField($model, 'alias'); ?>
                    <?php echo $form->error($model,'alias');?>
                </div>
            </div>
            <div class="da-form-col-4-8">
                    Dùng làm tên file. Format: không đấu viết liền. VD: HaiEmNuSinhDanhNhau
            </div>
        </div>
        
        <div class="da-form-row">
            <div class="da-form-col-4-8">
                <?php echo $form->labelEx($model,'file'); ?>
                <div class="da-form-item large">
                <?php 
//                $this->widget('CMultiFileUpload', array(
//                    'name' => 'file',
//                    'accept' => 'jar|3gp', // useful for verifying files
//                    'duplicate' => 'Duplicate file!', // useful, i think
//                    'denied' => 'Invalid file type', // useful, i think
//                ));
                ?>
                    <?php echo $form->fileField($model,'file', array('class' => 'da-custom-file', 'name' => 'file')); ?>
                </div>
            </div>
                <div class="da-form-col-4-8">
                       File Video hoặc Game dạng .3gp hoặc .jar
                       
                       

                    <?php if($this->action->id == 'update'):?>
                        <div><a href="<?php echo $model->fileUrl?>"><?php echo $model->fileUrl?></a></div>
                    <?php endif?> 

                       
                </div>
        </div>
        
        <style>
            .upload_method{
                cursor: pointer;
            }
            .upload_method.selected{
                text-decoration: underline;
                font-weight: bold;
            }
            #image_file, #image_url{
                display: none;
            }
        </style>
        <script>
            $(function(){
                    $("#a_url").click(function(){
                            $('#a_file').removeClass('selected');    
                            $('#a_url').addClass('selected');    
                            $('#image_url').show();    
                            $('#image_file').hide();    
                            $('#Movie_upload_method').val('url');    
                    });
                    $("#a_file").click(function(){
                            $('#a_url').removeClass('selected');    
                            $('#a_file').addClass('selected');    
                            $('#image_file').show();    
                            $('#image_url').hide();
                            $('#Movie_upload_method').val('file');    
                    });


                    $("#browse_file").change(function(evt){
                            var files = evt.target.files;
                            var f = files[0];

                            if(!f.type.match('image.*')) {
                                alert('File không hợp lệ. Hãy chọn 1 file ảnh khác.');
                                return false;   
                            }

                            var i = document.createElement('input');
                            if('multiple' in i){
                                var reader = new FileReader();
                                reader.readAsDataURL(f);
                                reader.onload = (function(){
                                        return function(e){
                                            $('#img_url').hide(); 
                                            $('#img_file').attr('src', e.target.result).show(); 
                                        };
                                })(f);
                                $('#img_review').show();
                            }
                    });      

                    $("#Movie_image_url").bind('change keyup blur', function(evt){
                            var method = $('#Movie_upload_method').val();
                            var ext = $(this).val().split('.').pop().toLowerCase(); 
                            if(method == 'url' && $.inArray(ext, [ 'jpg', 'gif', 'png' ] >= 0)){
                                $('#img_file').hide();
                                $('#img_url').attr('src', $(this).val()).show();
                                $('#img_review').show(); 

                            } 
                    });

                    <?php if($model->upload_method == 'file'):?>
                        $('#image_file').show();    
                        $('#image_url').hide(); 
                        $('#a_file').addClass('selected'); 
                        <?php else:?>
                        $('#image_url').show();    
                        $('#image_file').hide();
                        $('#a_url').addClass('selected');
                        <?php if($model->image_url):?>
                            $('#img_url').attr('src', '<?php echo $model->image_url?>').show();
                            <?php endif?> 
                    <?php endif?>
            });
        </script>
        <div class="da-form-row">
            <div class="da-form-col-4-8">
                <label>Upload ảnh file <span class="required">*</span> </label>
                <div class="da-form-item large">
                    <div style="float: left;">
                        <a id="a_file" class="upload_method">Từ máy tính</a> &nbsp;|&nbsp; 
                        <a id="a_url" class="upload_method">Từ URL</a>
                        <?php echo $form->hiddenField($model,'upload_method'); ?>
                    </div> 
                    <div style="clear: both;"></div>

                    <div id="image_file">
                        <?php echo $form->fileField($model,'image_file', array('class' => 'da-custom-file', 'name' => 'browse_file')); ?>
                    </div>
                    <div id="image_url">
                        <?php echo $form->textField($model,'image_url', array('placeholder' => 'http://domain.com/path/image.jpg')); ?>
                    </div>
                    <?php echo $form->error($model,'image_file');?>
                    <?php echo $form->error($model,'upload_method');?>
                </div>
            </div>
            <div class="da-form-col-3-8">
                <div class="da-form-item large" id="div_image_preview" style="margin-left: 0;">
                    <?php if($this->action->id == 'update'):?>
                        <img style="height: 60px; width: auto;" src="<?php echo $model->imageUrl.'?'.uniqid()?>" />
                        <?php else:?> 
                        <img id="img_file" style="display: none; height: 60px; width: auto;" /> 
                        <img id="img_url" style="display: none; height: 60px; width: auto;" />
                       <?php endif?> 
                </div>
            </div>
        </div>


        <div class="da-form-row">
            <div class="da-form-col-3-8">
                <label></label>
                <div class="da-form-item">
                    <button type="submit" class="da-button blue"><?php echo $model->isNewRecord ? 'Đăng file' : 'Sửa file'?></button>
                </div>
            </div>
        </div>
    </div>
    <?php $this->endWidget(); ?>
</div>